#
import serial, sys, os, time

def main():
    gps = serial.Serial("/dev/cuaU0", 4800, timeout=0)
    gpslog = open("/tmp/gpslog", "w")
    
    while 1:
	nmea = gps.readline()
	gps.flush()
	gpslog.write(nmea)
	gpslog.flush()
	time.sleep(0.3)
	
if __name__ == "__main__":
    try:
	pid = os.fork()
	if pid > 0:
	    # exit first parent
	    sys.exit(0)
    except OSError, e:
        print >>sys.stderr, "Fork #1 failed: %d (%s)" % (e.errno, e.strerror)
        sys.exit(1)
    # decouple from parent env
    os.chdir("/")
    os.setsid()
    os.umask(0)
    # second fork
    try:
        pid = os.fork()
        if pid > 0:
		print "Daemon PID %d" % pid
		sys.exit(0)
    except OSError, e:
        print >>sys.stderr, "Fork #1 failed: %d (%s)" % (e.errno, e.strerror)
        sys.exit(1)

main()
